package com.ascentive.finallyfast.quickboost;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.view.Gravity;
import android.widget.Toast;

import com.ascentive.finallyfast.R;
import com.ascentive.finallyfast.deepclean.DeepCleanPage;
import com.ascentive.finallyfast.deepclean.Utility;

public class MiscUtil {

    public static final int MENU_CANCEL = 0;
    public static final int MENU_SWITCH = 1;
    public static final int MENU_KILL = 2;
    public static final int MENU_DETAIL = 3;
    public static final int MENU_UNINSTALL = 4;

    public static PackageInfo getPackageInfo(PackageManager pm, String name) {
        PackageInfo ret = null;
        try {
            ret = pm.getPackageInfo(name, PackageManager.GET_ACTIVITIES);
        } catch (NameNotFoundException e) {
            // e.printStackTrace();
        }
        return ret;
    }
//
//    public static Dialog getTaskMenuDialog(final QuickBoostPage ctx, final QBDetailProcess dp) {
//
//        return new AlertDialog.Builder(ctx).setTitle(dp.getTitle()).setItems(
//                R.array.menu_task_operation, new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        switch (which) {
//                            case MENU_KILL: {
//                                ctx.am.restartPackage(dp.getPackageName());
//                                if (dp.getPackageName().equals(ctx.getPackageName())) return;
//                                ctx.refresh();
//                                return;
//                            }
//                            case MENU_SWITCH: {
//                                if (dp.getPackageName().equals(ctx.getPackageName())) return;
//                                Intent i = null;
//								try {
//									i = dp.getIntent();
//								} catch (NameNotFoundException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
//                                if (i == null) {
//                                    Toast.makeText(ctx, R.string.message_switch_fail, Toast.LENGTH_LONG)
//                                            .show();
//                                    return;
//                                }
//                                try {
//                                    ctx.startActivity(i);
//                                } catch (Exception ee) {
//                                    Toast.makeText(ctx, ee.getMessage(), Toast.LENGTH_LONG).show();
//                                }
//                                return;
//                            }
//                            case MENU_UNINSTALL: {
//                                Uri uri = Uri.fromParts("package", dp.getPackageName(), null);
//                                Intent it = new Intent(Intent.ACTION_DELETE, uri);
//                                try {
//                                    ctx.startActivity(it);
//                                } catch (Exception e) {
//                                    Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
//                                }
//                                return;
//                            }
//                            case MENU_DETAIL: {
//                                Intent detailsIntent = new Intent();
//                                detailsIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
//                                detailsIntent.putExtra("com.android.settings.ApplicationPkgName", dp.getPackageName());
//                                ctx.startActivity(detailsIntent);
//                                
//                                //Uri uri = Uri.parse("market://search?q=pname:" + );
//                                //Intent it = new Intent(Intent.ACTION_VIEW, uri);
//                                // try {
//                                // ctx.startActivity(detailsIntent);
//                                // } catch (Exception e) {
//                                // Toast.makeText(ctx, R.string.message_no_market,
//                                // Toast.LENGTH_LONG).show();
//                                // }
//                                return;
//                            }
//                        }
//
//                        /* User clicked so do some stuff */
//                        // String[] items =
//                        // ctx.getResources().getStringArray(R.array.menu_task_operation);
//                        // Toast.makeText(ctx, "You selected: " + which + " , " + items[which],
//                        // Toast.LENGTH_SHORT).show();
//                    }
//                }).create();
//    }
    public static AlertDialog getCloseAppDialog(final QuickBoostPage ctx, final QBDetailProcess dp) {

    	AlertDialog closeAppDialog = new AlertDialog.Builder(ctx).create();
    	closeAppDialog.setMessage("Do you want to close " + dp.getTitle() + " application?");
    	// Set positive button
    	 closeAppDialog.setButton("Yes",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							 // Close the Application 
							 ctx.am.restartPackage(dp.getPackageName());
                             if (dp.getPackageName().equals(ctx.getPackageName()))
                             {
                            	 MiscUtil.DisplayToast(ctx, "The "+ dp.getTitle() + " application cannot be closed");
                            	 return;
                             }
                             else
                             {
                            	 MiscUtil.DisplayToast(ctx, "The "+ dp.getTitle() + " application is closed");
                             }
                             ctx.refresh();
                             return;
						}
					});
			// Set negative button
    	 closeAppDialog.setButton2("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							return;
						}
					});
    	 return closeAppDialog;
    }	

    	
    
    public static long getDateBeforeNDays(long lDay)
    {
    	long yourDateMillis = System.currentTimeMillis() - (lDay * 24 * 60 * 60 * 1000);
    	// Time yourDate = new Time();
    	// yourDate.set(yourDateMillis);
    	// String formattedDate = yourDate.format("%Y-%m-%d %H:%M:%S");
    
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
    	String dateString = formatter.format(new Date(yourDateMillis)); // * 1000L));
    	
    	//textView = (TextView)findViewById(R.id.TextView01);
    	//textView.setText(dateString);
    	System.out.println("Past Date");
    	System.out.println(dateString);
    	
    	return (yourDateMillis);
    }
    public static long getDateBeforeNWeeks(long lWeek)
    {	
    	long yourDateMillis = System.currentTimeMillis() - ( (7 * lWeek) * 24 * 60 * 60 * 1000);
    	// Time yourDate = new Time();
    	// yourDate.set(yourDateMillis);
    	// String formattedDate = yourDate.format("%Y-%m-%d %H:%M:%S");
    	
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
    	String dateString = formatter.format(new Date(yourDateMillis)); // * 1000L));
    	
    	//textView = (TextView)findViewById(R.id.TextView01);
    	//textView.setText(dateString);
    	System.out.println("Past Date");
    	System.out.println(dateString);
    	
    	return (yourDateMillis);
    }
    public static long getDateBeforeNMonths(long lMonth)
    {
    	long yourDateMillis = System.currentTimeMillis() - ((30 * lMonth) * 24 * 60 * 60 * 1000);
    	// Time yourDate = new Time();
    	// yourDate.set(yourDateMillis);
    	// String formattedDate = yourDate.format("%Y-%m-%d %H:%M:%S");
    	
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
    	String dateString = formatter.format(new Date(yourDateMillis)); // * 1000L));
    	
    	//textView = (TextView)findViewById(R.id.TextView01);
    	//textView.setText(dateString);
    	System.out.println("Past Date");
    	System.out.println(dateString);
    	
    	return (yourDateMillis);
    }
    
    public static String convertToStringDateFromDouble(long dd)
    {
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
    	String dateString = formatter.format(new Date(dd)); // * 1000L));
    	
    	return dateString;
    }
    
 // Return Size of SD card
	public static long getSDCardMemory() {
		
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		long bytesAvailable = (long) stat.getBlockSize()
				* (long) stat.getBlockCount();
		long megAvailable = bytesAvailable / 1048576;
		System.out.println("Megs :" + megAvailable);
		
		return megAvailable;
	}
	
	public static float megabytesAvailable() {
	    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
	    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
	    return (bytesAvailable / (1024.f * 1024.f));
	}
	
	public static float megabytesTotal() {
	    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
	    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
	    return (bytesAvailable / (1024.f * 1024.f));
	}
	public static void DisplayToast(final QuickBoostPage ctx, String message) {
		Toast msg = Toast.makeText(ctx, message, Toast.LENGTH_LONG);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
				msg.getYOffset() / 2);

		msg.show();
	}
    
}
